﻿package com.shawnblais.pages
{
    import com.gskinner.motion.*;
    import com.shawnblais.events.*;
    import com.shawnblais.ui.*;
    
    import fl.motion.easing.*;
    
    import flash.display.*;
    import flash.events.*;
    import flash.filters.*;
    import flash.utils.*;
    
    import gs.TweenMax;
    import gs.easing.Quint;
    
    import org.papervision3d.cameras.*;
    import org.papervision3d.materials.*;
    import org.papervision3d.objects.*;
    import org.papervision3d.objects.primitives.*;
    import org.papervision3d.view.*;
    import org.papervision3d.view.layer.util.*;

    public class Page3D extends BasicView implements IPage
    {
        public var startButton_mc:mcStartButton;
        protected var _container:DisplayObject3D;
        protected var lookOffsetX:Number = 0;
        protected var aboutMenu:BasicMenu3D;
        protected var lookAtObject:DisplayObject3D;
        protected var dummyPlane:Plane;
        public var minHeight:Number = 650;
        protected var movingCamera:Boolean = false;
        protected var labMenu:BasicMenu3D;
        public var minWidth:Number = 1280;
        protected var contactMenu:ContactForm3D;
        public var useFullscreen:Boolean = true;
        protected var _aMenus:Object;
        protected var mainMenu:MainMenu3D;
        protected var portfolioMenu:BasicMenu3D;
        public var pageBorder_mc:MovieClip;
        public var useDepthOfField:Boolean = false;
        protected var lookAtTarget:DisplayObject3D;
        protected var currentMenu:BasicMenu3D;

        public function Page3D() : void
        {
            this._aMenus = new Array();
            super(this.minWidth, this.minHeight, false, true, CameraType.FREE);
            _camera.focus = 100;
            _camera.zoom = 10;
            _camera.z = -800;
            viewport.interactive = true;
            viewport.containerSprite.sortMode = ViewportLayerSortMode.Z_SORT;
            startRendering();
            
            return;
        }// end function

        protected function addMouseWheelListener():void
        {
            this.addEventListener(MouseEvent.MOUSE_WHEEL, onMouseScroll);
            return;
        }// end function

        public function init():void
        {
            var _loc_1:MovieClip = null;
            var _loc_2:MovieMaterial = null;
            if (!this._container)
            {
                this._container = new DisplayObject3D();
                scene.addChild(this._container);
                viewport.getChildLayer(this._container);
                this.startButton_mc = new mcStartButton();
                this.startButton_mc.useHandCursor = true;
              this.startButton_mc.alpha = 0;
                addChildAt(this.startButton_mc, 0);
                new GTween(this.startButton_mc, 1, {alpha:1, y:viewport.viewportHeight / 2}, {ease:Bounce.easeOut});
                _loc_1 = new MovieClip();
                _loc_1.graphics.beginFill(0, 0);
                _loc_1.graphics.drawRect(0, 0, this.startButton_mc.width, this.startButton_mc.height);
                _loc_1.graphics.endFill();
                _loc_2 = new MovieMaterial(_loc_1, true);
                this.dummyPlane = new Plane(_loc_2, this.startButton_mc.width, this.startButton_mc.height, 1, 1);
                this.dummyPlane.z = -200;
                _loc_2.interactive = true;
                scene.addChild(this.dummyPlane);
                _loc_1.addEventListener(MouseEvent.CLICK, showMenus);
                _loc_1.addEventListener(MouseEvent.MOUSE_OVER, this.onButtonOver);
                _loc_1.addEventListener(MouseEvent.MOUSE_UP, this.onButtonOut);
                this.lookAtObject = new DisplayObject3D();
                this.lookAtObject.z = 1000;
                this.lookAtTarget = new DisplayObject3D();
                scene.addChild(this.lookAtObject);
                this.pageBorder_mc = new MovieClip();
                this.pageBorder_mc.graphics.lineStyle(1, 13421772);
                this.pageBorder_mc.graphics.beginFill(0, 0);
                this.pageBorder_mc.graphics.drawRect((-this.minWidth) / 2, (-this.minHeight) / 2, this.minWidth, this.minHeight);
                this.pageBorder_mc.graphics.endFill();
                this.pageBorder_mc.x = this.pageBorder_mc.width / 2;
                this.pageBorder_mc.y = this.pageBorder_mc.height / 2;
                this.pageBorder_mc.mouseEnabled = false;
                this.pageBorder_mc.mouseChildren = false;
                this.pageBorder_mc.visible = false;
                addChild(this.pageBorder_mc);
            }
            return;
        }// end function

        override protected function onRenderTick(event:Event = null) : void
        {
            var _loc_2:Number = NaN;
            var _loc_3:Number = NaN;
            var _loc_4:* = undefined;
            var _loc_5:* = undefined;
            super.onRenderTick(event);
            if (!this.movingCamera)
            {
                if (stage.mouseX / stage.stageWidth >= 0)
                {
                }
                _loc_2 = stage.mouseX / stage.stageWidth <= 1 ? (stage.mouseX / stage.stageWidth) : (0.5);
                if (stage.mouseY / stage.stageHeight >= 0)
                {
                }
                _loc_3 = stage.mouseY / stage.stageHeight <= 1 ? (stage.mouseY / stage.stageHeight) : (0.5);
                _loc_4 = -100 + 200 * _loc_2;
                _loc_5 = -50 + 100 * _loc_3;
             // new GTween(this.lookAtObject, 0.5, {x:this.lookAtTarget.x + this.lookOffsetX + _loc_4, y:this.lookAtTarget.y - _loc_5}, {ease:Quartic.easeOut});
              
            TweenMax.to(this.lookAtObject, 0.5, {x:this.lookAtTarget.x + this.lookOffsetX + _loc_4, y:this.lookAtTarget.y - _loc_5,ease: gs.easing.Quint.easeOut})
          //  this.lookAtObject.x=this.lookAtTarget.x+ this.lookOffsetX + _loc_4
          //  this.lookAtTarget.y=this.lookAtTarget.y - _loc_5;
            
            }
            if (this.movingCamera)
            {
                this.setDepthOfField();
            }
            return;
        }// end function

        public function unload() : void
        {
            return;
        }// end function

        private function onButtonOut(event:Event):void
        {
            viewport.buttonMode = true;
            return;
        }// end function

        private function onButtonSelected(event:Event):void
        {
            var _loc_2:BasicMenu3D = null;
            var _loc_3:uint = 0;
            var _loc_4:Number = NaN;
            var _loc_5:Number = NaN;
            var _loc_6:Number = NaN;
            if (event.target === this.currentMenu)
            {
                _loc_2 = BasicMenu3D(event.target);
                _loc_3 = Math.ceil(_loc_2.length / 2);
                _loc_4 = 300;
                _loc_5 = _loc_4 / _loc_2.length;
                if (_loc_2.selectedIndex == _loc_3)
                {
                    _loc_6 = 0;
                }
                else if (_loc_2.selectedIndex > _loc_3)
                {
                    _loc_6 = (1 - (_loc_2.length - _loc_2.selectedIndex) / _loc_3) * (-_loc_4);
                }
                else
                {
                    _loc_6 = (1 - _loc_2.selectedIndex / _loc_3) * _loc_4;
                }
                this.lookOffsetX = _loc_6;
                new GTween(this.lookAtObject, 0.5, {x:this.lookAtTarget.x + this.lookOffsetX}, {ease:Quartic.easeOut});
            }
            return;
        }// end function

        public function setDepthOfField():void
        {
            var _loc_2:Number = NaN;
            var _loc_3:Number = NaN;
            var _loc_4:Number = NaN;
            var _loc_1:int = 0;
            while (_loc_1 < this._aMenus.length)
            {
                
                if (this.useDepthOfField)
                {
                    _loc_2 = camera.z - this._aMenus[_loc_1].z;
                    _loc_3 = camera.y - this._aMenus[_loc_1].y;
                    _loc_4 = Math.sqrt(_loc_2 * _loc_2 + _loc_3 * _loc_3) / 350 - 5;
                    this._aMenus[_loc_1].container.filters = [new BlurFilter(_loc_4, _loc_4, 3)];
                }
                else
                {
                    this._aMenus[_loc_1].container.filters = [];
                }
                _loc_1++;
            }
            return;
        }// end function

        public function transitionOut() : void
        {
            return;
        }// end function

        private function lookAtMenu(param1):void
        {
            var _loc_2:Number = 0.7;
           camera.target = this.lookAtObject;
           this.lookAtTarget.copyTransform(param1);
         this.lookAtTarget.moveForward(500);
        //  new GTween(this.lookAtObject, _loc_2, {x:this.lookAtTarget.x, y:this.lookAtTarget.y, z:this.lookAtTarget.z}, {ease:Cubic.easeInOut});
          
          TweenMax.to(this.lookAtObject, _loc_2, {x:this.lookAtTarget.x, y:this.lookAtTarget.y, z:this.lookAtTarget.z,ease: gs.easing.Cubic.easeInOut})
          
         

            return;
        }// end function

        private function cameraTweenFinished(event:Event=null):void
        {
            this.movingCamera = false;
            return;
        }// end function

        public function transitionIn() : void
        {
            var _loc_2:* = undefined;
            var _loc_4:GTween = null;
            var _loc_5:int = 0;
            var _loc_1:Array = [];
            var _loc_3:int = 0;
            while (_loc_3 < this._aMenus.length)
            {
                
                if (_loc_3 == 0)
                {
                    _loc_1.push(this._aMenus[0]);
                }
                else
                {
                    _loc_5 = 0;
                    while (_loc_5 < _loc_1.length)
                    {
                        
                        if (this._aMenus[_loc_3].z > _loc_1[_loc_5].z)
                        {
                            _loc_2 = this._aMenus[_loc_3];
                            break;
                        }
                        else if (!_loc_2 && _loc_5 == _loc_1.length--)
                        {
                            _loc_2 = this._aMenus[_loc_3];
                        }
                        _loc_5++;
                    }
                    if (_loc_2)
                    {
                        _loc_1.splice(_loc_5, 0, _loc_2);
                    }
                }
                _loc_3++;
            }
            this.movingCamera = true;
            _camera.z = _loc_1[0].z;
            
            TweenMax.to(_camera, 1, {x:_loc_1[_loc_1.length-1].x - 250, y:_loc_1[_loc_1.length-1].y + 150, z:_loc_1[_loc_1.length-1].z - 2000,ease: gs.easing.Back.easeOut});
            
            //new GTween(_camera, 1, {x:_loc_1[_loc_1.length-1].x - 250, y:_loc_1[_loc_1.length-1].y + 150, z:_loc_1[_loc_1.length-1].z - 2000}, {ease:Back.easeOut});
          //  _loc_4 = new GTween(_camera, 0.55, {x:_loc_1[_loc_1.length-1].x, y:_loc_1[_loc_1.length-1].y, z:_loc_1[_loc_1.length-1].z - 1000}, {ease:Back.easeOut, delay:1});
         //   _loc_4.addEventListener(Event.COMPLETE, this.cameraTweenFinished);
            
             TweenMax.to(_camera, 0.55, {x:_loc_1[_loc_1.length-1].x, y:_loc_1[_loc_1.length-1].y, z:_loc_1[_loc_1.length-1].z - 1000,ease:gs.easing.Back.easeOut,onComplete:cameraTweenFinished, delay:1});
            
            
            setTimeout(this.lookAtMenu, 1500, this.mainMenu);
            return;
        }// end function

        protected function setMenuListeners(param1:Boolean = true):void
        {
            var _loc_2:int = 0;
            while (_loc_2 < this._aMenus.length)
            {
                
                if (param1)
                {
                    this._aMenus[_loc_2].addEventListener(MenuEvent.BUTTON_SELECTED, this.onButtonSelected);
                    this._aMenus[_loc_2].addEventListener(MenuEvent.BUTTON_CLICKED, this.onButtonClicked);
                }
                else
                {
                    this._aMenus[_loc_2].removeEventListener(MenuEvent.BUTTON_SELECTED, this.onButtonSelected);
                    this._aMenus[_loc_2].removeEventListener(MenuEvent.BUTTON_CLICKED, this.onButtonClicked);
                }
                _loc_2++;
            }
            return;
        }// end function

        private function onButtonOver(event:Event):void
        {
            viewport.buttonMode = true;
            return;
        }// end function

        public function Destroy() : void
        {
            return;
        }// end function

        private function onMouseScroll(event:MouseEvent):void
        {
            var _loc_2:* = event.delta;
            if (_loc_2 > 0)
            {
                this.currentMenu.next(true);
            }
            else
            {
                this.currentMenu.previous(true);
            }
            this.removeEventListener(MouseEvent.MOUSE_WHEEL, this.onMouseScroll);
            setTimeout(this.addMouseWheelListener, 1);
            return;
        }// end function

        private function onButtonClicked(event:Event)
        {
            var cameraTween:GTween;
            var selectedIndex:*;
            var e:* = event;
            if (!this.movingCamera)
            {
                if (e.target === this.currentMenu)
                {
                    this.movingCamera = true;
                    selectedIndex = e.target.selectedIndex;
                    this.currentMenu.selectPlane();
                    this.currentMenu.setButtonListeners(false);
                    this.currentMenu.container.buttonMode = false;
                    if (this.currentMenu != this.mainMenu)
                    {
                        this.currentMenu = this.mainMenu;
                       // cameraTween = new GTween(_camera, 1, {y:this.mainMenu.y, x:this.mainMenu.x, z:this.mainMenu.z - 1000}, {ease:Back.easeOut});
                     TweenMax.to(_camera, 1, {y:this.mainMenu.y, x:this.mainMenu.x, z:this.mainMenu.z - 1000, ease:gs.easing.Back.easeOut, onComplete:cameraTweenFinished});
                    }
                    else
                    {
                        this.currentMenu = this._aMenus[selectedIndex + 1];
                       // cameraTween = new GTween(_camera, 1, {y:this.currentMenu.y, x:this.currentMenu.x, z:this.currentMenu.z - 1000}, {ease:Back.easeIn});
                   TweenMax.to(_camera, 1, {y:this.currentMenu.y, x:this.currentMenu.x, z:this.currentMenu.z - 1000, ease: gs.easing.Back.easeIn ,onComplete:cameraTweenFinished});
                  
                  
                    }
                    if(cameraTween!=null)
                    {
                   cameraTween.addEventListener(Event.COMPLETE, cameraTweenFinished);
                   }
                    setTimeout(function ()
            {
                currentMenu.selectPlane(currentMenu.restoreIndex);
                return;
            }// end function
            , 500);
                    this.currentMenu.setButtonListeners();
                    this.lookAtMenu(this.currentMenu);
                }
            }
            return;
        }// end function

        private function showMenus(event:MouseEvent = null) : void
        {
            var _loc_2:* = undefined;
            if (!this.mainMenu)
            {
                new GTween(this.startButton_mc, 0.3, {scaleX:8, scaleY:8}, {ease:Quadratic.easeOut});
                new GTween(this.startButton_mc, 0.35, {scaleX:0, scaleY:0, alpha:0}, {ease:Quadratic.easeOut, delay:0.25});
                scene.removeChild(this.dummyPlane);
                _loc_2 = [{y:1600, x:1450, z:1200}, {y:-850, x:2000, z:1500}, {y:-1000, x:-1200, z:2000}, {y:1000, x:-700, z:2000}];
                this.mainMenu = new MainMenu3D(viewport, 0, this._container);
                this.mainMenu.buildMenu("MainHeader", [{src:"Main_Portfolio", color:39218}, {src:"Main_About", color:16750898}, {src:"Main_Lab", color:10040115}, {src:"Main_Contact", color:13158}, {src:"Main_OG", color:0, url:"http://www.shawnblais.com/v1"}]);
                this.mainMenu.createViewportLayer(viewport, true);
                this._aMenus.push(this.mainMenu);
                this.currentMenu = this.mainMenu; 
              this.portfolioMenu = new BasicMenu3D(viewport, -1, this._container);
                this.portfolioMenu.enableBackButton = true;
                this.portfolioMenu.buildMenu("PortfolioHeader", [{src:"Portfolio_Dynamic", title:"Modern Granite", url:"http://www.moderngranite.com/index_main.php", color:39218, frameNum:1}, {src:"Portfolio_Dynamic", title:"NAEJA", url:"http://www.scopeserver.com/~naejaco", color:39218, frameNum:2}, {src:"Portfolio_Dynamic", title:"Cosmetology Connection", url:"http://www.cosmetologyconnection.ca/index_tmp.php", color:39218, frameNum:3}, {src:"Portfolio_Dynamic", title:"Noralta", url:"http://www.noralta.com/", color:39218, frameNum:4}, {src:"Portfolio_Dynamic", title:"Alberta Chiro", url:"http://www.albertachiro.com/", color:39218, frameNum:5}, {src:"Portfolio_Dynamic", title:"Madison\'s Grill", url:"http://www.unionbankinn.com/index_madison.php", color:39218, frameNum:6}, {src:"Portfolio_Dynamic", title:"Coronet", url:"http://www.coronetequipment.com/", color:39218, frameNum:7}, {src:"Portfolio_Dynamic", title:"oomRoom", url:"http://www.oomroom.ca", color:39218, frameNum:8}, {src:"Portfolio_Dynamic", title:"Stage 2 Clothing", url:"http://www.stage2clothing.ca", color:39218, frameNum:9}, {src:"Portfolio_Dynamic", title:"Union Bank Inn", url:"http://www.unionbankinn.com", color:39218, frameNum:10}, {src:"Portfolio_Dynamic", title:"Learning Strategies", url:"http://www.oomroom.ca/resources/strategies", color:39218, frameNum:11}, {src:"Portfolio_Dynamic", title:"Canadian Chiro", url:"http://www.chiroconvention.com/", color:39218, frameNum:12}, {src:"Portfolio_Dynamic", title:"Adler Insulation", url:"http://www.adlerinsulation.com/", color:39218, frameNum:13}]);
                this.portfolioMenu.setButtonListeners(false);
                this.portfolioMenu.createViewportLayer(viewport);
                this.portfolioMenu.y = _loc_2[0].y;
                this.portfolioMenu.x = _loc_2[0].x;
                this.portfolioMenu.z = _loc_2[0].z;
                this._aMenus.push(this.portfolioMenu);
                this.aboutMenu = new BasicMenu3D(viewport, -1, this._container);
                this.aboutMenu.numPlaneSegments = 4;
                this.aboutMenu.enableBackButton = true;
                this.aboutMenu.hideContent = true;
                this.aboutMenu.buildMenu("AboutHeader", [{src:"About_Content", color:16750898}]);
                this.aboutMenu.setButtonListeners(false);
                this.aboutMenu.createViewportLayer(viewport);
                this.aboutMenu.y = _loc_2[1].y;
                this.aboutMenu.x = _loc_2[1].x;
                this.aboutMenu.z = _loc_2[1].z;
                this._aMenus.push(this.aboutMenu);
                this.labMenu = new BasicMenu3D(viewport, -1, this._container);
                this.labMenu.enableBackButton = true;
                this.labMenu.buildMenu("LabHeader", [{src:"Lab_Dynamic", title:"Cover Flow", color:10040115, url:"http://www.shawnblais.com/lab/?page_id=2", frameNum:1}, {src:"Lab_Dynamic", title:"ColorTween", color:10040115, url:"http://www.shawnblais.com/lab/?page_id=13", frameNum:2}, {src:"Lab_Dynamic", title:"Framerate Monitor", color:10040115, url:"http://www.shawnblais.com/lab/?page_id=22", frameNum:3}]);
                this.labMenu.setButtonListeners(false);
                this.labMenu.createViewportLayer(viewport);
                this.labMenu.y = _loc_2[3].y;
                this.labMenu.x = _loc_2[3].x;
                this.labMenu.z = _loc_2[3].z;
                this._aMenus.push(this.labMenu);
                this.contactMenu = new ContactForm3D(viewport, -1, this._container);
                this.contactMenu.hideContent = true;
                this.contactMenu.enableBackButton = true;
                this.contactMenu.buildMenu("ContactHeader", [{src:"Contact_Form", color:13158}]);
                this.contactMenu.setButtonListeners(false);
                this.contactMenu.createViewportLayer(viewport);
                this.contactMenu.y = _loc_2[2].y;
                this.contactMenu.x = _loc_2[2].x;
                this.contactMenu.z = _loc_2[2].z;
                this._aMenus.push(this.contactMenu); 
                this.setMenuListeners();
                this.addMouseWheelListener();
               this.transitionIn(); 
            }
            return;
        }// end function

    }
}
